package com.delio.client.login;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
//import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.ProgressBar;
//import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
//import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Timer; 

public class UsuarioForm extends Dialog{

    TextField<String> us;
    TextField<String> ps;
    Button ac,cn;
    FormPanel f;
  //  private Status status;
  //  private ButtonBar buttonBar;
    
    public UsuarioForm(){
    	
    	GXT.hideLoadingPanel("loading");
       	addForm();
        setBodyBorder(false);  
	    setHeading("Login");
	    setIconStyle("images/icons/user.png");
	    setWidth(280);  
	    setHeight(140);
	    setButtons("");
	    setFrame(true);
	    setClosable(false);
	    setPlain(true);
	    add(f);
	    addButton(ac);
	    addButton(cn);
	    
	    
	   /* buttonBar = new ButtonBar();  
        buttonBar.setMinButtonWidth(75);  
	    
        buttonBar.add(new Button("Progress", new SelectionListener<ButtonEvent>() {  
            public void componentSelected(ButtonEvent ce) {  
              final MessageBox box = MessageBox.progress("Please wait", "Loading items...",  
                  "Initializing...");  
              final ProgressBar bar = box.getProgressBar();  
              final Timer t = new Timer(){
                float i;  
                @Override  
                public void run() {  
                  bar.updateProgress(i / 100, (int) i + "% Complete");  
                  i += 5;  
                  if (i > 105) {  
                    cancel();  
                    box.close();  
                    Info.display("Message", "Items were loaded", "");  
                  }  
                }  
              };  
              t.scheduleRepeating(500);  
            }  
          }));*/  
	}
    
    public void addForm(){
    	f = new FormPanel();
    	f.setHeaderVisible(false);
      	us = new TextField<String>();
 	    us.setFieldLabel("Usuario");
 	    us.setAllowBlank(false);
 	    us.setMinLength(5);
 	    
 	    us.addKeyListener(new KeyListener(){
 		public void componentKeyDown(ComponentEvent event){
 			if(event.getKeyCode() == KeyCodes.KEY_ENTER){	
 			  if(us.isValid()){ ps.focus();}  
 			  else {us.clear(); us.focus();} 
 				
 			}
 		 }
 	    });
 	 	ps = new TextField<String>();
 	 	ps.setPassword(true);
 	 	ps.setFieldLabel("Password");
 	 	ps.setAllowBlank(false);
 	 	
 	 	ps.addKeyListener(new KeyListener(){
 			public void componentKeyDown(ComponentEvent event){
 				if(event.getKeyCode() == KeyCodes.KEY_ENTER){									
 					ps.focus();
 				}
 			 }
 		    });
 	 	ac  = new Button("Aceptar");

 	 	ac.addSelectionListener(new SelectionListener<ButtonEvent>() {
 		      public void componentSelected(ButtonEvent ce) {
 		    //	 status();
 			//     status.show();
 		    	 final Usuario u = new Usuario();
 		    	 u.setUsuario(us.getValue());
 		    	 u.setPassword(ps.getValue());
 //////////////////////////////////////////////////////////////////		    	 
 		         final MessageBox box = MessageBox.progress("Porfavor espere", "Cargando...","Inicializando...");  
                 final ProgressBar bar = box.getProgressBar();  
                 final Timer t = new Timer(){
                  float i;  
                 @Override  
                 public void run() {  
                   bar.updateProgress(i / 100, (int) i + "% Completando");  
                   i += 5;  
                   if (i > 105) {  
                     cancel();  
                     box.close();  
                	 Usuarios.validar(u);
    		    	 Usuarios.uf = UsuarioForm.this;
     		    
                   }  
                 }  
               };  
               t.scheduleRepeating(500);  
/////////////////////////////////////////////////////////////////// 		    	 
 		    	// Usuarios.validar(u);
		    	// Usuarios.uf = UsuarioForm.this;
 		      }
 		  });
 	 	cn = new Button("Cancelar");
 	 	cn.addSelectionListener(new SelectionListener<ButtonEvent>(){
 	 		public void componentSelected(ButtonEvent ce){
 	 		   cleanForm();
 	 		   us.focus();
 	 		}
 	 	});
   	 	f.add(us,new FormData("-20"));
 	 	f.add(ps,new FormData("-20"));
    }
    
    public void cleanForm(){
    	us.clear();
    	ps.clear();
    }
/*
    public void status(){
      ac.setEnabled(false);
      cn.setEnabled(false);
      status = new Status();
	  status.setBusy("Verificando...");
	  status.hide();
	  status.setAutoWidth(true);
	  getButtonBar().add(status);
	  getButtonBar().add(new FillToolItem());
    }
  */  

    

}